import { ref } from "vue";
import { loadProductsAPI } from "../services/products";

export function useProduct() {
  const products = ref([]);
  const page = ref(1);
  const loading = ref(false);
  const finished = ref(false);

  //加载数据 并实现加载更多
  const loadData = (category = "", needReset = false) => {
    if (needReset) {
      products.value = [];
      page.value = 1;
      finished.value = false;
    }
    loading.value = true;
    loadProductsAPI(page.value, category).then((res) => {
      if (res.pages < page.value) {
        finished.value = true;
      }
      products.value.push(...res.data);
      page.value++;
      loading.value = false;
    });
  };
  return {
    products,
    loadData,
    finished,
    loading,
  };
}
